package com.maket.jbxx.controller;

import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.CodeClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.maket.jbxx.domain.KsMerchants;
import com.maket.jbxx.service.IKsMerchantsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 客商管理Controller
 * 
 * @author ruoyi
 * @date 2024-07-01
 */
@RestController
@RequestMapping("/merchants")
public class KsMerchantsController extends BaseController
{
    @Autowired
    private IKsMerchantsService ksMerchantsService;

    @Resource
    private CodeClient codeClient;
    /**
     * 查询客商管理列表
     */
    @RequiresPermissions("jbxx:merchants:list")
    @GetMapping("/list")
    public TableDataInfo list(KsMerchants ksMerchants)
    {
        startPage();
        List<KsMerchants> list = ksMerchantsService.selectKsMerchantsList(ksMerchants);
        return getDataTable(list);
    }

    /**
     * 导出客商管理列表
     */
    @RequiresPermissions("jbxx:merchants:export")
    @Log(title = "客商管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KsMerchants ksMerchants)
    {
        List<KsMerchants> list = ksMerchantsService.selectKsMerchantsList(ksMerchants);
        ExcelUtil<KsMerchants> util = new ExcelUtil<KsMerchants>(KsMerchants.class);
        util.exportExcel(response, list, "客商管理数据");
    }

    /**
     * 获取客商管理详细信息
     */
    @RequiresPermissions("jbxx:merchants:query")
    @GetMapping(value = "/{ksCode}")
    public AjaxResult getInfo(@PathVariable("ksCode") String ksCode)
    {
        return success(ksMerchantsService.selectKsMerchantsByKsCode(ksCode));
    }

    /**
     * 新增客商管理
     */
    @RequiresPermissions("jbxx:merchants:add")
    @Log(title = "客商管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KsMerchants ksMerchants)
    {
        ksMerchants.setKsCode(codeClient.getCode(8));

        return toAjax(ksMerchantsService.insertKsMerchants(ksMerchants));
    }

    /**
     * 修改客商管理
     */
    @RequiresPermissions("jbxx:merchants:edit")
    @Log(title = "客商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KsMerchants ksMerchants)
    {
        return toAjax(ksMerchantsService.updateKsMerchants(ksMerchants));
    }


    /**
     * 修改客商状态
     */
    @RequiresPermissions("jbxx:merchants:edit")
    @Log(title = "客商管理", businessType = BusinessType.UPDATE)
    @PutMapping("isStart")
    public AjaxResult editIsStart(@RequestBody KsMerchants ksMerchants)
    {
        return toAjax(ksMerchantsService.updateKsMerchantsIsStart(ksMerchants));
    }

    /**
     * 删除客商管理
     */
    @RequiresPermissions("jbxx:merchants:remove")
    @Log(title = "客商管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ksCodes}")
    public AjaxResult remove(@PathVariable String[] ksCodes)
    {
        return toAjax(ksMerchantsService.deleteKsMerchantsByKsCodes(ksCodes));
    }
}
